/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestion;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import otros.MiTableModel;
import tablasSql.Familias;
import tablasSql.Sql;

/**
 *
 * @author FERRETERIA
 */
public class FFamilia extends javax.swing.JInternalFrame {

    /**
     * Creates new form FFamilia
     */
    public FFamilia() {
        initComponents();
        tabla.setAutoCreateRowSorter(true);
        AnchoColumnas();
        ActualizarTabla(); 
        txtId.setVisible(false);
    }

    private void AnchoColumnas() {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel)tabla.getColumnModel();
        TableColumn col = colModel.getColumn(2);
        col.setPreferredWidth(500);
        colModel.getColumn(0).setMaxWidth(0);
        colModel.getColumn(0).setMinWidth(0);
        colModel.getColumn(0).setPreferredWidth(0);
    }
    
     private void ActualizarTabla(){                    
        // se colocan los datos en la tabla
        String[] columNames = {"Id","Codigo","Descripcion"};  
        Object [][] dtDatos = Familias.getFamilias(txtBuscar.getText());
        MiTableModel datos = new MiTableModel(dtDatos,columNames);
        tabla.setModel(datos);         
        AnchoColumnas();
        txtAGuardar.setText("");
        txtCodigo.setText("");
        txtId.setText("");
        bBorrar.setEnabled(false);
        bGuardar.setEnabled(false);
        txtAGuardar.requestFocus();
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        bGuardar = new javax.swing.JButton();
        txtCodigo = new javax.swing.JTextField();
        txtAGuardar = new javax.swing.JTextField();
        bBorrar = new javax.swing.JButton();
        txtId = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtBuscar = new javax.swing.JTextField();
        bBuscar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Gestion de Familias");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/img/icono2.png"))); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setPreferredSize(new java.awt.Dimension(352, 250));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Codigo", "Descripcion"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);
        if (tabla.getColumnModel().getColumnCount() > 0) {
            tabla.getColumnModel().getColumn(0).setResizable(false);
            tabla.getColumnModel().getColumn(1).setResizable(false);
            tabla.getColumnModel().getColumn(2).setResizable(false);
        }

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridheight = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 10);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jPanel1.setPreferredSize(new java.awt.Dimension(460, 70));

        bGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/guardar.png"))); // NOI18N
        bGuardar.setToolTipText("Guardar");
        bGuardar.setMinimumSize(new java.awt.Dimension(60, 60));
        bGuardar.setPreferredSize(new java.awt.Dimension(61, 61));
        bGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGuardarActionPerformed(evt);
            }
        });
        jPanel1.add(bGuardar);

        txtCodigo.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCodigo.setPreferredSize(new java.awt.Dimension(40, 20));
        txtCodigo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCodigoMouseClicked(evt);
            }
        });
        txtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCodigoKeyReleased(evt);
            }
        });
        jPanel1.add(txtCodigo);

        txtAGuardar.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtAGuardar.setPreferredSize(new java.awt.Dimension(250, 20));
        txtAGuardar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtAGuardarMouseClicked(evt);
            }
        });
        txtAGuardar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtAGuardarKeyReleased(evt);
            }
        });
        jPanel1.add(txtAGuardar);

        bBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/eliminar.png"))); // NOI18N
        bBorrar.setToolTipText("Borrar");
        bBorrar.setMinimumSize(new java.awt.Dimension(60, 60));
        bBorrar.setPreferredSize(new java.awt.Dimension(60, 60));
        bBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBorrarActionPerformed(evt);
            }
        });
        jPanel1.add(bBorrar);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        getContentPane().add(jPanel1, gridBagConstraints);

        txtId.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtId.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        getContentPane().add(txtId, gridBagConstraints);

        jPanel2.setPreferredSize(new java.awt.Dimension(360, 70));

        jLabel1.setText("Familia:");
        jPanel2.add(jLabel1);

        txtBuscar.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtBuscar.setPreferredSize(new java.awt.Dimension(200, 20));
        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtBuscarKeyPressed(evt);
            }
        });
        jPanel2.add(txtBuscar);

        bBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        bBuscar.setToolTipText("Buscar");
        bBuscar.setMinimumSize(new java.awt.Dimension(60, 60));
        bBuscar.setPreferredSize(new java.awt.Dimension(60, 60));
        bBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bBuscarMouseClicked(evt);
            }
        });
        jPanel2.add(bBuscar);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        getContentPane().add(jPanel2, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bBuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bBuscarMouseClicked
        ActualizarTabla();
    }//GEN-LAST:event_bBuscarMouseClicked

    private void txtBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyPressed
       if (evt.getKeyCode() == 10)
          ActualizarTabla();
    }//GEN-LAST:event_txtBuscarKeyPressed

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked
        int fila = tabla.rowAtPoint(evt.getPoint());
        int columna = tabla.columnAtPoint(evt.getPoint());
        if ((fila > -1) && (columna > -1)) {
            if (tabla.getValueAt(fila,columna)!= null) {
                txtId.setText(tabla.getValueAt(fila,0).toString());
                txtCodigo.setText (tabla.getValueAt(fila,1).toString());
                txtAGuardar.setText(tabla.getValueAt(fila,2).toString());
                bBorrar.setEnabled(true);
                bGuardar.setEnabled(true);
            }
        }
    }//GEN-LAST:event_tablaMouseClicked

    private void txtCodigoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoKeyReleased
        if (evt.getKeyCode() == 10) {
            if ((!txtCodigo.getText().isEmpty()) && (!txtAGuardar.getText().isEmpty())) {
                bGuardar.setEnabled(true);
            }
        }
    }//GEN-LAST:event_txtCodigoKeyReleased

    private void txtAGuardarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAGuardarKeyReleased
        if (evt.getKeyCode() == 10) {
            if ((!txtCodigo.getText().isEmpty()) && (!txtAGuardar.getText().isEmpty())) {
                bGuardar.setEnabled(true);
            }
        }
    }//GEN-LAST:event_txtAGuardarKeyReleased

    private void txtCodigoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCodigoMouseClicked
        if (txtCodigo.getText().isEmpty()) {
            String sql = "select codigo as max from nfamilias where id in (select max(id) from nfamilias)";
            txtCodigo.setText(String.valueOf(Sql.getMax(sql)+1));
        }
    }//GEN-LAST:event_txtCodigoMouseClicked

    private void txtAGuardarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtAGuardarMouseClicked
        if (txtCodigo.getText().isEmpty()) 
            txtCodigoMouseClicked(null);
    }//GEN-LAST:event_txtAGuardarMouseClicked

    private void bBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBorrarActionPerformed
        if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "¿Esta seguro de borrar?","Borrar", JOptionPane.OK_CANCEL_OPTION)) {
            Familias.borrar(txtId.getText());
            ActualizarTabla();
        }
    }//GEN-LAST:event_bBorrarActionPerformed

    private void bGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGuardarActionPerformed
        if (txtAGuardar.getText().trim().compareTo("") != 0) {
            if (txtId.getText().compareTo("") == 0) {
                String datos [] = {String.valueOf(Sql.getNextId("nfamilias")),
                                   txtCodigo.getText(),
                                   txtAGuardar.getText()};        
                Familias.guardar(datos, true);
            }
            else {
                String datos [] = {txtAGuardar.getText(), txtCodigo.getText(), txtId.getText()};
                Familias.guardar(datos, false);
            }
            ActualizarTabla();
        }
    }//GEN-LAST:event_bGuardarActionPerformed

    public static void main(String args[]) {
    
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FFamilia().setVisible(true);
            }
        });
    }

    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBorrar;
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField txtAGuardar;
    private javax.swing.JTextField txtBuscar;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtId;
    // End of variables declaration//GEN-END:variables
}
